package com.amigo.online.provider.merchant.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.core.security.IgnoreSecurity;
import com.amigo.online.common.param.dto.merchant.CommentReplyDto;
import com.amigo.online.common.param.dto.merchant.EditBasicMerchantDto;
import com.amigo.online.common.param.dto.merchant.MerchantApplyDto;
import com.amigo.online.common.param.dto.merchant.MerchantAvatarDto;
import com.amigo.online.common.param.dto.merchant.MerchantCommentDto;
import com.amigo.online.common.param.dto.merchant.MerchantEnvironmentDto;
import com.amigo.online.common.param.result.merchant.CategoryInfo;
import com.amigo.online.common.param.result.merchant.MerchantCommentInfo;
import com.amigo.online.common.param.result.merchant.MerchantManagerInfo;
import com.amigo.online.provider.merchant.exception.MerchantException;
import com.amigo.online.provider.merchant.service.CategoryService;
import com.amigo.online.provider.merchant.service.GoodsCommentService;
import com.amigo.online.provider.merchant.service.MerchantManagerService;
import com.amigo.online.provider.merchant.service.MerchantService;
import com.amigo.online.provider.merchant.service.MerchantStatusService;
import com.amigo.online.provider.merchant.utils.LanguageEnum;

/**
 * <code>MerchantOwnerController</code>
 * <p>
 * : 商家(拥有者视角)controller
 * </p>
 * <p>
 *
 * @author LuChen
 * @version 1.0
 * @company Amigo
 * @time 2018年12月15日 下午12:43:01
 * @lastUpdate 无
 * </p>
 */
@RestController
@RequestMapping(value = "/merOwner", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class MerchantOwnerController extends BaseController {

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private MerchantManagerService merchantManagerService;

    @Autowired
    private MerchantStatusService merchantStatusService;

    @Autowired
    private GoodsCommentService goodsCommentService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取大类列表
     *
     * @param language
     * @param type     1-首页大类  2-申请店铺大类
     * @return
     */
    @GetMapping("/categoryList")
    public ResponseEntity<Response> getCategoryList(@RequestHeader("Accept-Language") String language,
                                                    @RequestParam(value = "type", required = false) Integer type) {
        language = LanguageEnum.getColumnName(language);
        List<CategoryInfo> list = categoryService.getCategoryList(language);
        type = type == null ? 1 : 2;
        //申请店铺的大类不需要"全部"
        if (type == 2) {
            for (CategoryInfo categoryInfo : list) {
                //id为1的类别是"全部"
                if (categoryInfo.getId() == 1) {
                    list.remove(categoryInfo);
                    break;
                }
            }
        }
        return success(list);
    }

    /**
     * 提交商户资料申请开通
     *
     * @return
     */
    @PostMapping("/applyOpen")
    public ResponseEntity<Response> applyOpenMerchant(@RequestBody MerchantApplyDto dto,
                                                      @RequestHeader("Accept-Language") String language) {
        language = LanguageEnum.getColumnName(language);
        return success(merchantService.applyOpenMerchant(dto, language));
    }

    /**
     * 修改店铺头像
     *
     * @return
     */
    @PostMapping("/merchant/avatar")
    public ResponseEntity<Response> editMerchant(@RequestBody MerchantAvatarDto dto) {
        boolean rs = merchantService.editMerchantAvatar(dto);
        return success(rs);
    }

    /**
     * 添加或修改店铺环境
     *
     * @return
     */
    @PostMapping("/merchant/environment")
    public ResponseEntity<Response> merchantEnvironment(@RequestBody MerchantEnvironmentDto dto) {
        boolean rs = merchantService.merchantEnvironment(dto);
        return success(rs);
    }

    /**
     * 修改店铺营业时间、服务电话、门店地址
     *
     * @return
     */
    @PostMapping("/merchant/basic")
    public ResponseEntity<Response> editBasicMerchant(@RequestBody EditBasicMerchantDto dto) {
        // 1-营业时间修改 2-服务电话修改 3-门店地址修改
        if (dto.getType() == 1) {
            if (dto == null || StringUtils.isBlank(dto.getStartTime()) || StringUtils.isBlank(dto.getEndTime())
                    || dto.getStartWeek() == null || dto.getEndWeek() == null) {
                throw new MerchantException(StatusCode.BAD_REQUEST);
            }
        }
        if (dto.getType() == 2) {
            if (dto == null || StringUtils.isBlank(dto.getMerchantPhoneNumber())) {
                throw new MerchantException(StatusCode.BAD_REQUEST);
            }
        }
        if (dto.getType() == 3) {
            if (dto == null || StringUtils.isBlank(dto.getStoreAdderss()) || dto.getStoreAreaId() == null) {
                throw new MerchantException(StatusCode.BAD_REQUEST);
            }
        }
        boolean rs = merchantService.editBasicMerchant(dto);
        return success(rs);
    }

    /**
     * 获取商家评论列表
     *
     * @return
     */
    @PostMapping("/getMerchantComment")
    public ResponseEntity<Response> getMerchantComment(@RequestBody MerchantCommentDto dto) {
        List<MerchantCommentInfo> info = goodsCommentService.getCommentMerList(dto);
        return success(info);
    }

    /**
     * 获取商家管理成员列表（不包含店长）
     *
     * @return
     */
    @GetMapping("/getMerchantManagerList")
    public ResponseEntity<Response> getManagerList(@RequestParam("merchantId") Long merchantId) {
        List<MerchantManagerInfo> info = merchantManagerService.getManagerList(merchantId);
        return success(info);
    }

    @GetMapping("/addManager")
    public ResponseEntity<Response> addManager(@RequestParam("merchantId") Long merchantId,
                                               @RequestParam("userId") Long userId) {
        boolean info = merchantManagerService.addManager(merchantId, userId);
        return success(info);
    }

    @DeleteMapping("/removeManager")
    public ResponseEntity<Response> removeManager(@RequestParam("merchantId") Long merchantId,
                                                  @RequestParam("userId") Long userId) {
        boolean info = merchantManagerService.removeManager(merchantId, userId);
        return success(info);
    }

    @PostMapping("/replyComment")
    public ResponseEntity<Response> replyComment(@RequestBody CommentReplyDto dto) {
        JSONObject info = merchantManagerService.reply(dto);
        return success(info);
    }

    /**
     * 获得用户店铺状态
     *
     * @return 0-初始用户 1-审核失败 2-审核中 3-已经是商家 4-该用户是管理员
     */
    @GetMapping("/getUserMerchantStatus")
    public ResponseEntity<Response> getUserMerchantInfo() {
        return success(merchantStatusService.getMerchantStatus());
    }

    /**
     * 根据店铺状态获取店铺信息
     */
    @GetMapping("/getUserMerchantInfo/{status}")
    public ResponseEntity<Response> getUserMerchantStatus(@PathVariable("status") Integer status,
                                                          @RequestHeader("Accept-Language") String language) {
        language = LanguageEnum.getColumnName(language);
        return success(merchantStatusService.getMerchantInfoByStatus(language, status));
    }

    /**
     * 根据店铺id 获得店铺的详细信息
     *
     * @return
     */
    @GetMapping("/getMerchantManagerInfo/{id}")
    public ResponseEntity<Response> getMerchantDetailInfo(@PathVariable("id") Long id,
                                                          @RequestHeader("Accept-Language") String language) {
        language = LanguageEnum.getColumnName(language);
        return success(merchantService.getMerchantDetailInfo(id, language));
    }

    /**
     * 商户端获得商品信息
     *
     * @return
     */
    @IgnoreSecurity
    @GetMapping("/merchantGoods")
    public ResponseEntity<Response> getMerchantGoods(@RequestParam(name = "merchantId") Long merchantId,
                                                     @RequestParam(name = "status", required = false) Integer status,
                                                     @RequestParam(name = "categoryId", required = false) Long categoryId, @RequestParam Integer page,
                                                     @RequestParam Integer size, @RequestHeader(name = "Accept-Language", required = false) String language) {
        return success(merchantService.getMerchantGoods(merchantId, status, categoryId, page, size,
                LanguageEnum.getColumnName(language)));
    }

    @DeleteMapping("/quitManager")
    public ResponseEntity<Response> quitManager(@RequestParam("merchantId") Long merchantId) {
        boolean info = merchantManagerService.quitManager(merchantId);
        return success(info);
    }

}
